package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.entity.BaseMembershipGradeEntity;
import com.yuanfeng.base.entity.BaseTagClassEntity;
import com.yuanfeng.base.mapper.BaseTagClassMapper;
import com.yuanfeng.base.service.BaseMembershipGradeService;
import com.yuanfeng.base.service.BaseTagClassService;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class BaseTagClassServiceImpl extends ServiceImpl<BaseTagClassMapper, BaseTagClassEntity>
    implements BaseTagClassService{

    @Autowired
    private BaseMembershipGradeService baseMembershipGradeService;

    @Override
    public ResponseResult getMemberClassIdByUserId(String userId) {
        BaseTagClassEntity entity=this.baseMapper.selectOne(new LambdaQueryWrapper<BaseTagClassEntity>()
                                 .eq(BaseTagClassEntity::getMemberId,userId));
        return ResponseResult.success(entity);
    }

    @Override
    public ResponseResult updateTagClassByUserId(Map<String, Object> map) {
        QueryWrapper<BaseMembershipGradeEntity> queryWrapper = new QueryWrapper<>();

        if (null != map && null != map.get("gradeStart")) {
            queryWrapper.le("grade_start", map.get("gradeStart").toString());

        }

        if (null != map && null != map.get("userIntegral")) {
            queryWrapper.le("grade_rule", map.get("userIntegral").toString());
            queryWrapper.orderByDesc("grade_rule");
            queryWrapper.last("limit 1");
        }

        //获取积分信息
        BaseMembershipGradeEntity membershipGradeEntity = baseMembershipGradeService.getOne(queryWrapper);

        //根据用户id更新会员等级
        this.baseMapper.update(null,new LambdaUpdateWrapper<BaseTagClassEntity>()
                       .eq(BaseTagClassEntity::getMemberId,map.get("userId").toString())
                       .set(BaseTagClassEntity::getMemberClassId,membershipGradeEntity.getId()));

        return ResponseResult.success();
    }

    @Override
    public ResponseResult saveTagClass(Map<String, Object> map) {
        BaseTagClassEntity tagClass = new BaseTagClassEntity();
        tagClass.setMemberId(map.get("userId").toString());
        //查询所有的会员等级
        List<BaseMembershipGradeEntity> membershipGradesList =baseMembershipGradeService.list(new LambdaQueryWrapper<BaseMembershipGradeEntity>()
                .eq(BaseMembershipGradeEntity::getGradeStart, 1)
                // 排序的目的是高等级覆盖低等级
                .orderByAsc(BaseMembershipGradeEntity::getGradeRule));

        //遍历所有的会员等级 判断用户注册所赠送的积分会使用户的等级升到几级
        Integer size = membershipGradesList.size() - 1;
        Integer ruleRegister = (Integer) map.get("ruleRegister");
        for (int i = size; i >0; i--) {
            Integer gradeRule = membershipGradesList.get(i - 1).getGradeRule();
            if ( ruleRegister >= gradeRule) {
                tagClass.setMemberClassId(membershipGradesList.get(i - 1).getId());
            }
        }
        //添加用户等级关联表
        int flag = this.baseMapper.insert(tagClass);

        return flag == CommonStatusEnum.SUCCESS.getCode() ? ResponseResult.success() : ResponseResult.fail();
    }
}




